﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEE.Model
{
    public class BPile :MemberBase
    {
        double Length;      // 总桩长，从cut off level，mm为单位
        int DiaPile = 1000; // 桩径
        int Cover = 75;     // 保护层
        int Phi;
        int LapLength = 300;    // 搭接长度
        int ringSpace = 2000;   // 加强筋间距
        int STSpaceTop = 150;   // 顶部螺旋筋间距
        int STSpaceMitUp = 250; // 中上部螺旋筋间距
        int STSpaceMitLo = 500; // 中下部螺旋筋间距
        int STSpaceBot = 500;   // 底部螺旋筋间距
        int LapLen2528 = 1530;  // 25和28钢筋搭接长度
        int LapLen2025 = 1050;  // 20和25钢筋搭接长度
        int LapLen2020 = 1050;  // 20和20钢筋搭接长度
        int LapLen1212 = 480;   // 12和12钢筋搭接长度
        int LapLen1612 = 480;   // 16和12钢筋搭接长度
        int LapLen1616 = 640;   // 16和16钢筋搭接长度
        int FirstLen = 12000;   // 默认第一根主筋长度,12m
        int SecLen = 12000;     // 默认第二根主筋长度,12m
        int ThirdLen = 12000;   // 默认第三根主筋长度,12m
        int DowelLen = 1400;    // 进入承台长度,1.4m
        int FirstTieLen = 6500; // 第一段6.5m加密区域
        int SecTieLen = 8000;   // 第二段8m加密区域


        int Len1b, Len2b, Len3b;
        //声明与内加强筋有关的参数变量
        int PhiTop, PhiMitUp, PhiMitLo, PhiBot; //外径
        int LenTop, LenMitUp, LenMitLo, LenBot; //长度
        int NumTopRing, NumMitUpRing, NumMitLoRing, NumBotRing; //长度
        //声明螺旋筋有关的参数变量
        int TopSpiral;
        int LenD;
        eSubType SubType;

        /// <summary>
        /// Type 4 摩擦桩的钢筋计算表
        /// 桩基主筋由12m通长钢筋搭配相应短筋和外部螺旋筋以及内部加强箍筋组成.
        /// </summary>
        /// <param name="length">桩长，总长，含进入承台长度</param>
        public BPile(string typePile, string pierNum, eSubType subType,double length, int lenD)
            :base(typePile, pierNum)
        {
            SubType = subType;
            Length = length * 1000;
            LenD = lenD;
            #region 主筋部分 1-3b系列(不通长)
            int Len1b = FirstTieLen + DowelLen;
            int Len2b = SecTieLen + FirstTieLen + DowelLen - FirstLen;
            int Len3b = (int)Length + DowelLen - FirstLen - SecLen - ThirdLen + LapLen2528 + LapLen2025 + LapLen2020 - Cover;
            #endregion

            #region 加强筋部分
            // 首层加强筋外径 : 桩基直径 - 2*保护层-2*外螺旋筋直径 - 2*主筋直径
            PhiTop = DiaPile - 2 * Cover - 2 * Convert.ToInt32(eDB.DB28) - 2 * Convert.ToInt32(eDB.DB16);   //对应7a
            PhiMitUp = DiaPile - 2 * Cover - 2 * Convert.ToInt32(eDB.DB28) - 2 * Convert.ToInt32(eDB.DB12); //对应7b
            PhiMitLo = DiaPile - 2 * Cover - 2 * Convert.ToInt32(eDB.DB25) - 2 * Convert.ToInt32(eDB.DB12); //对应7c
            PhiBot = DiaPile - 2 * Cover - 2 * Convert.ToInt32(eDB.DB20) - 2 * Convert.ToInt32(eDB.DB12);   //对应7d
            //计算对应加强筋的总长度 : (外径 - 自身直径)
            LenTop = (int)Math.Round((PhiTop - Convert.ToInt32(eDB.DB20)) * Math.PI + LapLength, 3, MidpointRounding.AwayFromZero);
            LenMitUp = (int)Math.Round((PhiMitUp - Convert.ToInt32(eDB.DB20)) * Math.PI + LapLength, 3, MidpointRounding.AwayFromZero);
            LenMitLo = (int)Math.Round((PhiMitLo - Convert.ToInt32(eDB.DB20)) * Math.PI + LapLength, 3, MidpointRounding.AwayFromZero);
            LenBot = (int)Math.Round((PhiBot - Convert.ToInt32(eDB.DB20)) * Math.PI + LapLength, 3, MidpointRounding.AwayFromZero);
            //计算对应加强筋的数量
            NumTopRing = (int)Math.Round((double)(FirstTieLen + LapLen2528-Cover) / ringSpace,3,MidpointRounding.ToEven);
            NumMitUpRing = (int)Math.Round((double)(FirstLen - LapLen2528 - DowelLen - 6500) / ringSpace, 3, MidpointRounding.AwayFromZero);
            NumMitLoRing = (int)Math.Round((double)(SecLen - LapLen2528) / ringSpace, 3, MidpointRounding.AwayFromZero);
            NumBotRing = (int)Math.Round((double)(Length - FirstLen - SecLen + LapLen2528 - LapLen2025) / ringSpace, 3, MidpointRounding.AwayFromZero);
            #endregion

            #region
            TopSpiral = 12000; // 顶部第一段螺旋筋12m

            #endregion
            // 
        }

        public override void SetConcreteVolume()
        {
            //混凝土体积
            int vol = (int)Math.Round(Length * Math.Pow(DiaPile,2) / 4 * Math.PI,3,MidpointRounding.AwayFromZero);
        }

        public override void SetRebarList()
        {
            if (Length <= FirstTieLen + SecTieLen)
            {
                throw new Exception();
            }
            else
            {
                RebarList = new RebarCollection();
                int diaOfN1;
                if(SubType == eSubType.A)//Type 1 
                {
                    diaOfN1 = 20;
                    RebarList.Add(new MARebarRecord(1, 0, "A", eDB.DB20, 12000, 20, eShapeType.MA));
                    RebarList.Add(new MARebarRecord(1, 0, "B", eDB.DB20, 6500 + LenD, 10, eShapeType.MA));


                }
                // 1a, 1b                
                RebarList.Add(new MARebarRecord(1, 0, "a", eDB.DB28, FirstLen, 0, LapLen2528, DowelLen, 16, eShapeType.Pile1a));    //1a 12m通长
                RebarList.Add(new MARebarRecord(1, 0, "b", eDB.DB28, Len1b, 0, 0, 0, 10, eShapeType.Pile1b));                       //1b 长度
                // 2a, 2b
                RebarList.Add(new MARebarRecord(2, 0, "a", eDB.DB25, SecLen, LapLen2528, LapLen2025, 0, 10, eShapeType.Pile1a));    //2a 12m通长
                RebarList.Add(new MARebarRecord(2, 0, "b", eDB.DB25, Len2b, LapLen2528, 0, 0, 6, eShapeType.Pile1b));               //2b 长度
                RebarList.Add(new MARebarRecord(3, 0, "a", eDB.DB20, ThirdLen, LapLen2025, LapLen2020, 0, 10, eShapeType.Pile1a));
                RebarList.Add(new MARebarRecord(3, 0, "b", eDB.DB20, Len3b, LapLen2020, 0, 0, 10, eShapeType.Pile1b));
                                        
                RebarList.Add(new MYRebarRecord(7, 0, "a", eDB.DB20, LenTop,LapLength,PhiTop,NumTopRing,eShapeType.MY));            //7a 加强筋
                RebarList.Add(new MYRebarRecord(7, 0, "b", eDB.DB20, LenMitUp, LapLength, PhiMitUp, NumMitUpRing, eShapeType.MY));  //7b 加强筋
                RebarList.Add(new MYRebarRecord(7, 0, "c", eDB.DB20, LenMitLo, LapLength, PhiMitLo, NumMitLoRing, eShapeType.MY));  //7c 加强筋
                RebarList.Add(new MYRebarRecord(7, 0, "d", eDB.DB20, LenBot, LapLength, PhiBot, NumBotRing, eShapeType.MY));        //7d 加强筋

                //RebarList.Add(new MZRebarRecord(4,0,"a",eDB.DB16,6245,LapLen1616,850))
                //RebarList.Add(new MZRebarRecord()

                


            }
        }


        public override void SetConcreteClass()
        {
            ConcreteClass = eConcreteClass.C30;
        }


        public void CountStirrupLength()
        {
            double dia = 0.85;

            double L = Length*0.001-0.075*2-6.425-8.0;
    
            double KK = L / L12AASHTO(dia, 0.5, eDB.DB12, eDB.DB12, eDB.DB12);

            double LKK=L - L12AASHTO(dia, 0.5, eDB.DB12, eDB.DB12, eDB.DB12) * Math.Floor(KK);

            double last = S2L(dia, 0.5, LKK / 0.5) + 1.5 * dia * Math.PI + 0.15 + 0.24;

        }

        




        
        public static double S2L(double OD,double S,double n)
        {
            return n * Math.Sqrt(S * S + Math.PI * OD * Math.PI * OD);
        }

        /// <summary>
        /// 12m螺旋筋的理论总螺旋长
        /// </summary>
        /// <param name="OD"></param>
        /// <param name="S"></param>
        /// <param name="before"></param>
        /// <param name="cur"></param>
        /// <param name="after"></param>
        /// <returns></returns>
        public static double L12(double OD,double S,eDB before,eDB cur,eDB after)
        {
            double lap1, lap2;
            if (before==eDB.DB16 && cur==eDB.DB16)
            {
                lap1 = 0.64;
            }
            else
            {
                lap1 = 0.48;
            }
            if (after == eDB.DB16 && cur == eDB.DB16)
            {
                lap2 = 0.64;
            }
            else
            {
                lap2 = 0.48;
            }

            return S * (12.0 - (lap1 + lap2) * 0.5) / Math.Sqrt( Math.Pow(S, 2) + Math.Pow(OD*Math.PI, 2));
        }
        public static double L12AASHTO(double OD, double S, eDB before, eDB cur, eDB after)
        {
            double lap1, lap2;
            if (before == eDB.DB16 && cur == eDB.DB16)
            {
                lap1 = 0.64;
            }
            else
            {
                lap1 = 0.48;
            }
            if (after == eDB.DB16 && cur == eDB.DB16)
            {
                lap2 = 0.64;
            }
            else
            {
                lap2 = 0.48;
            }
            double dd = Convert.ToInt32(cur) * 0.001;
            return (12 - (lap1+lap2)*0.5)/Math.Sqrt((Math.Pow((OD-0.012)*Math.PI,2)+Math.Pow(S,2)))*S;
           // return (12 - (lap1 + lap2) * 0.5 - 8 * dd) / (Math.PI*(OD+ dd))*S;
        }
    }
}
